import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:school_assistant/post_page.dart';
import 'package:school_assistant/register_page.dart';
import 'package:school_assistant/shiwu_page.dart';
import 'package:school_assistant/shop_page.dart';

import 'home_page.dart';
import 'info_page.dart';
import 'login_page.dart';
import 'package:sp_util/sp_util.dart';

import 'my_article.dart';
import 'order_list.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await SpUtil.getInstance();
  runApp(const MyApp());
  //设置statusBar 黑体并且透明
  const style = SystemUiOverlayStyle(
    statusBarColor: Colors.transparent,
    statusBarIconBrightness: Brightness.dark,
  );
  SystemChrome.setSystemUIOverlayStyle(style);
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      title: '校园助手',
      initialRoute: '/home',
      debugShowCheckedModeBanner: false,
      getPages: [
        GetPage(name: "/", page: () => getPage('home')),
        GetPage(name: "/login", page: () => getPage('login')),
        GetPage(name: "/register", page: () => getPage('register')),
        GetPage(name: "/post", page: () => getPage('post')),
        GetPage(name: "/shop", page: () => getPage('shop')),
        GetPage(name: "/order", page: () => getPage('order')),
        GetPage(name: "/my_article", page: () => getPage('my_article')),
        GetPage(name: "/my_info", page: () => getPage('my_info')),
        GetPage(name: "/shiwu", page: () => getPage('shiwu')),
      ],
      theme: ThemeData(
        primarySwatch: Colors.green,
      ),
      builder: EasyLoading.init(),
    );
  }

  Widget getPage(String pageName) {
    if (SpUtil.getString("uid") == null ||
        SpUtil.getString("uid")?.isEmpty == true) {
      return LoginPage();
    }
    if (pageName == 'home') {
      return HomePage();
    } else if (pageName == 'register') {
      return RegisterPage();
    } else if (pageName == 'post') {
      return PostPage();
    } else if (pageName == 'shop') {
      return ShopPage();
    } else if (pageName == 'order') {
      return OrderList();
    } else if (pageName == 'my_article') {
      return MyActicle();
    } else if (pageName == 'my_info') {
      return PriInfoPage();
    } else if (pageName == 'shiwu') {
      return ShiwuPage();
    }
    return HomePage();
  }
}
